'use strict';

const BaseService = require('./base');

class LeaseService extends BaseService {
  get _model() {
    return 'Lease';
  }

  async findOne({ id }) {
    const { app } = this;
    const where = this.formatWhere({ exact: { id } });
    const include = [
      {
        model: app.model.User,
        attributes: {
          exclude: app.exclude.concat([ 'password' ]),
        },
        include: [
          {
            model: app.model.UserInfo,
            attributes: {
              exclude: app.exclude,
            },
            include: [
              {
                model: app.model.Area,
                attributes: [ 'name' ],
              },
            ],
          },
        ],
      },
      {
        model: app.model.House,
        attributes: {
          exclude: app.exclude,
        },
        include: [
          {
            model: app.model.Area,
            attributes: [ 'name' ],
          },
          {
            model: app.model.HouseInfo,
            attributes: {
              exclude: app.exclude,
            },
          },
          {
            model: app.model.User,
            as: 'Owner',
            attributes: {
              exclude: app.exclude.concat([ 'password' ]),
            },
            include: [
              {
                model: app.model.UserInfo,
                attributes: {
                  exclude: app.exclude,
                },
                include: [
                  {
                    model: app.model.Area,
                    attributes: [ 'name' ],
                  },
                ],
              },
            ],
          },
        ],
      },
    ];
    return this._findOne({ where, include });
  }

  async create({ startTime, endTime, uid, houseId, status }) {
    return this._create({
      startTime, endTime, uid, houseId, status,
    });
  }

  async update({ status, id }) {
    const where = { id };
    const body = { status };
    return this._update({ body, where });
  }

  async findList({ uid, houseId, oid, status, pageIndex, pageSize, createdAt, prop, sort }) {
    const { app } = this;
    const where = this.formatWhere({
      range: { createdAt },
      exact: { uid, houseId, status },
    });
    const whereHouseInfo = this.formatWhere({ exact: { oid } });
    const order = this.formatOrder(prop, sort);
    const include = [
      {
        model: app.model.User,
        attributes: [ 'nickName' ],
      },
      {
        model: app.model.House,
        attributes: [ 'name' ],
        where: whereHouseInfo,
        include: [
          {
            model: app.model.User,
            as: 'Owner',
            attributes: [ 'nickName' ],
          },
        ],
      },
    ];
    return this._findList({ pageIndex, pageSize, where, order, include });
  }

  async findAll({ startTime, endTime, status } = {}) {
    const where = this.formatWhere({ exact: { status, startTime }, lt: { endTime } });
    return this._findAll({ where });
  }
}

module.exports = LeaseService;
